<template>
  <a-modal
    :title="title"
    :width="modalWidth"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form" ref="formRef">
        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="板号">
          <a-input placeholder="请输入板号" v-decorator="['version', validatorRules.version]" />
        </a-form-item>

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="格子数量">
          <a-input-number
            style="width: 100%"
            placeholder="请输入格子数量"
            v-decorator="['quantity', validatorRules.quantity]"
          />
        </a-form-item>

        <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="格子类型">
          <j-dict-select-tag
            allowClear
            placeholder="请选择"
            :triggerChange="true"
            dictCode="cellType"
            v-decorator="['type', validatorRules.type]"
          />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { postAction } from '@/api/manage'

export default {
  name: 'CellBatchAdd',
  data() {
    return {
      title: '批量新增格子',
      modalWidth: 800,
      visible: false,
      confirmLoading: false,
      validatorRules: {
        version: {
          rules: [
            {
              required: true,
              message: '请输入板号!',
            },
          ],
        },
        quantity: {
          rules: [
            {
              required: true,
              message: '请输入格子数量!',
            },
          ],
        },
        type: {
          rules: [
            {
              required: true,
              message: '请选择格子类型!',
            },
          ],
        },
      },
      confirmDirty: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      form: this.$form.createForm(this),
      url: 'business/machineCell/batchAdd',
      record: {},
    }
  },
  methods: {
    handleCancel() {
      this.close()
    },
    async showModal(record) {
      this.visible = true
      const cellNum = localStorage.getItem('cellNum')
      console.log(cellNum, 'cellNum')
      this.record = record
      this.$nextTick(() => {
        this.form.setFieldsValue({ quantity: cellNum })
      })
    },
    close() {
      this.record = {}
      this.visible = false
    },
    handleOk() {
      if (!this.record.id) {
        that.$message.warning('请选择机器再操作！')
      }
      const that = this
      // 触发表单验证
      this.form.validateFields((err, values) => {
        if (!err) {
          that.confirmLoading = true
          let params = Object.assign({}, values, { id: that.record.deviceId })
          //改动
          // localStorage.setItem('cellNum', values.quantity)
          // that.close()
          // this.$emit('refresh')
          // that.confirmLoading = false

          postAction(this.url, params)
            .then((res) => {
              if (res.success) {
                localStorage.setItem('cellNum', values.quantity)
                that.$message.success(res.message)
                that.close()
                this.$emit('refresh')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
            })
        }
      })
    },
  },
}
</script>

<style scoped></style>

